package com.guoxin.part1.chapter3;

import java.util.concurrent.TimeUnit;

/**
 * 捕获中断信号关闭线程
 *
 * @author guoxin
 * @date 2019/8/27
 */
public class InterruptThreadExit {

    public static void main(String[] args) throws InterruptedException {
        Thread t = new Thread(){
            @Override
            public void run() {
                System.out.println("I will start work");
                while (!isInterrupted()){

                }
                System.out.println("I will be exiting.");
            }
        };
        t.start();
        TimeUnit.SECONDS.sleep(5);
        System.out.println("System will be shutdown");
        t.interrupt();
    }
}
